package com.proposition.commons.exam.util;

import com.proposition.commons.exam.exception.ExamImportException;
import com.proposition.commons.exam.result.ResultEnum;
import com.proposition.commons.model.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author: 陈玉林
 * @modifiedBy:
 * @date: Create In 10:16 2017/12/1
 * @description:
 */
public class ExamImportUtil {
    private static int tableSize = 0;

    /**
     * @author: 陈玉林
     * @date: 2017/12/1 10:22
     * @description: 解析上传的试题文件 2007版
     */
    public static List<Map<String, String>> parseUploadFileByWord2007(String filePath, Integer categoryId) {

        // 用来保存所有试题
        List<Map<String, String>> exams = new ArrayList<>(40);
        try {
            XWPFDocument document = new XWPFDocument(new FileInputStream(filePath));
            // 获取所有表格
            List<XWPFTable> tables = document.getTables();
            //验证试题分类
            for (XWPFTableRow row :  tables.get(0).getRows()) {
                List<XWPFTableCell> tableCells = row.getTableCells();
                for (XWPFTableCell cell : tableCells) {
                    String s = cell.getText().trim();
                    if (!s.contains("分类-")) {
                        throw new ExamImportException(ResultEnum.ILLEGAL_CATEGORY);
                    }else if (!ExamUtil.getCategory(s.substring(s.indexOf("-") + 1)).equals(categoryId)) {
                        throw new ExamImportException(ResultEnum.ILLEGAL_CATEGORY);
                    }
                }
            }
            for (XWPFTable table : tables) {
                // 用来保存单个试题
                Map<String, String> exam = new TreeMap<String, String>();
                // 获取表格的行
                List<XWPFTableRow> rows = table.getRows();
                for (XWPFTableRow row : rows) {
                    // 获取表格的每个单元格
                    List<XWPFTableCell> tableCells = row.getTableCells();
                    for (XWPFTableCell cell : tableCells) {
                        // 获取单元格的内容
                        String s = cell.getText().trim();
                        System.out.println(s.length()==0);
                        if (s != null && s.length()!=0) {
                            //非答案主治项 s.split("：")[1]不可为空
                            if (s.contains("：")) {
                                String[] split = s.split("：");
                                //确保数组长度大于1
                                if (split.length > 0) {
                                    if (split.length > 1) {
                                        exam.put(split[0], s.substring(s.indexOf("：") + 1).trim());
                                    } else {
                                        exam.put(split[0], "");
                                    }
                                }
                            }
                        }else{
                            throw new ExamImportException(ResultEnum.EMPTY_EXAM_TABLE_FIELD);
                        }
                    }
                }
                if (!exam.isEmpty()) {
                    exams.add(exam);
                }
            }
            tableSize = tables.size();
            //表的size不得小于2
            if (tableSize < 2) {
                throw new ExamImportException(ResultEnum.EMPTY_EXAMS);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new ExamImportException(ResultEnum.UNKONW_ERROR);
        }
        return exams;
    }
    /**
     *@author: 陈玉林
     *@date: 2017/12/1 10:22
     *@description: 解析上传的试题文件 2003版
     */
    public static List<Map<String, String>> parseUploadFile(String filePath, Integer categoryId) {
        // 用来保存所有试题
        List<Map<String, String>> exams = new ArrayList<Map<String, String>>();
        try {
            FileInputStream in = new FileInputStream(filePath);//载入文档
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            HWPFDocument hwpf = new HWPFDocument(pfs);
            Range range = hwpf.getRange();//得到文档的读取范围
            TableIterator it = new TableIterator(range);
            //迭代文档中的表格
            int count = 0;
            while (it.hasNext()) {
                count++;
                Table table = (Table) it.next();
                // 用来保存单个试题
                Map<String, String> exam = new TreeMap<String, String>();
                //迭代行，默认从0开始
                for (int i = 0; i < table.numRows(); i++) {
                    TableRow tr = table.getRow(i);
                    //迭代列，默认从0开始
                    for (int j = 0; j < tr.numCells(); j++) {
                        TableCell td = tr.getCell(j);//取得单元格
                        //取得单元格的内容
                        for (int k = 0; k < td.numParagraphs(); k++) {
                            Paragraph para = td.getParagraph(k);
                            String s = para.text();
                            s = s.substring(0, s.length() - 1);

                            // 验证试题分类
                            if (s.contains("分类-")) {
                                if (!ExamUtil.getCategory(s.substring(s.indexOf("-") + 1)).equals(categoryId)) {
                                    throw new ExamImportException(ResultEnum.ILLEGAL_CATEGORY);
                                }

                            } else {
                                //非答案主治项 s.split("：")[1]不可为空
                                if (s.contains("：")) {
                                    String[] split = s.split("：");
                                    //确保数组长度大于1
                                    if (split.length > 0) {
                                        if (split.length > 1) {
                                            exam.put(split[0], s.substring(s.indexOf("：") + 1).trim());
                                        } else {
                                            exam.put(split[0], "");
                                        }
                                    }
                                }
                            }
                        } //end for
                    }   //end for
                }
                if (!exam.isEmpty()) {
                    exams.add(exam);
                }//end for
            }
            //表的size不得小于2
            if (count < 2) {
                throw new ExamImportException(ResultEnum.EMPTY_EXAMS);
            }//end while
        } catch (Exception e) {
            e.printStackTrace();
            throw new ExamImportException(ResultEnum.UNKONW_ERROR);
        }
        return exams;
    }

    /**
     * @author: 陈玉林
     * @date: 2017/12/15 16:23
     * @description: 获取导入模板分类名称
     */
    public static String getTemplateCategoryName(Integer categoryId) {
        String name = "";
        switch (categoryId) {
            case 1:
                name = "分类-问答题-腧穴";
                break;
            case 2:
                name = "分类-问答题-内科";
                break;
            case 3:
                name = "分类-问答题-伤科";
                break;
            case 4:
                name = "分类-问答题-妇科";
                break;
            case 5:
                name = "分类-问答题-儿科";
                break;
            case 6:
                name = "分类-问答题-触诊";
                break;
            case 7:
                name = "分类-穴位定位-儿科穴位";
                break;
            case 8:
                name = "分类-穴位定位-成人穴位";
                break;
            case 9:
                name = "分类-单项手法-儿科手法";
                break;
            case 10:
                name = "分类-单项手法-成人手法";
                break;
            case 11:
                name = "分类-单项手法-触诊诊断";
                break;
            case 12:
                name = "分类-成套手法-内科";
                break;
            case 13:
                name = "分类-成套手法-伤科";
                break;
            case 14:
                name = "分类-成套手法-妇科";
                break;
            case 15:
                name = "分类-成套手法-儿科";
                break;
            default:
                break;
        }
        return name;
    }

    /**
     * 穴位点我-成人穴位
     * 将终极表试题的值给予一审表试题
     * @param exam1 终极表试题
     * @param exam2 一审表试题
     * @return
     */
    public static ExamAcupointAdultOriginal setValueIntoExamOriginal(ExamAcupointAdult exam1, ExamAcupointAdultOriginal exam2) {

        exam2.setId(exam1.getId());
        exam2.setStem(exam1.getStem());
        exam2.setTotalScore(exam1.getTotalScore());
        exam2.setCheckStatusId(exam1.getCheckStatusId());
        exam2.setCategoryId(exam1.getCategoryId());
        exam2.setOutlineCode(exam1.getOutlineCode());
        exam2.setKnowledgePoint(exam1.getKnowledgePoint());
        exam2.setCognitiveLevel(exam1.getCognitiveLevel());
        exam2.setUserIdImport(exam1.getUserIdImport());
        exam2.setPartIdAdult(exam1.getPartIdAdult());
        exam2.setPartScoreAdult(exam1.getPartScoreAdult());
        exam2.setMeridianAdult(exam1.getMeridianAdult());
        exam2.setMeridianScoreAdult(exam1.getMeridianScoreAdult());
        exam2.setLocation1Adult(exam1.getLocation1Adult());
        exam2.setLocation2Adult(exam1.getLocation2Adult());
        exam2.setLocation3Adult(exam1.getLocation3Adult());
        exam2.setLocation1ScoreAdult(exam1.getLocation1ScoreAdult());
        exam2.setLocation2ScoreAdult(exam1.getLocation2ScoreAdult());
        exam2.setLocation3ScoreAdult(exam1.getLocation3ScoreAdult());
        return exam2;
    }
    /**
     * 穴位点我-儿科穴位
     * 将终极表试题的值给予一审表试题
     * @param exam1 终极表试题
     * @param exam2 一审表试题
     * @return
     */
    public static ExamAcupointPediatricOriginal setValueIntoExamOriginal(ExamAcupointPediatric exam1, ExamAcupointPediatricOriginal exam2) {
        exam2.setId(exam1.getId());
        exam2.setStem(exam1.getStem());
        exam2.setTotalScore(exam1.getTotalScore());
        exam2.setCheckStatusId(exam1.getCheckStatusId());
        exam2.setCategoryId(exam1.getCategoryId());
        exam2.setOutlineCode(exam1.getOutlineCode());
        exam2.setKnowledgePoint(exam1.getKnowledgePoint());
        exam2.setCognitiveLevel(exam1.getCognitiveLevel());
        exam2.setUserIdImport(exam1.getUserIdImport());
        exam2.setPartIdPediatric(exam1.getPartIdPediatric());
        exam2.setPartScorePediatric(exam1.getPartScorePediatric());
        exam2.setPartDetailPediatric(exam1.getPartDetailPediatric());
        exam2.setPartDetailScorePediatric(exam1.getPartDetailScorePediatric());
        exam2.setLocation1Pediatric(exam1.getLocation1Pediatric());
        exam2.setLocation2Pediatric(exam1.getLocation2Pediatric());
        exam2.setLocation3Pediatric(exam1.getLocation3Pediatric());
        exam2.setLocation1ScorePediatric(exam1.getLocation1ScorePediatric());
        exam2.setLocation2ScorePediatric(exam1.getLocation2ScorePediatric());
        exam2.setLocation3ScorePediatric(exam1.getLocation3ScorePediatric());
        return exam2;
    }

    /**
     * 问答题-腧穴
     * 将终极表试题的值给予一审表试题
     * @param exam1 终极表试题
     * @param exam2 一审表试题
     * @return
     */
    public static ExamAcupointOriginal setValueIntoExamOriginal(ExamAcupoint exam1, ExamAcupointOriginal exam2) {
        exam2.setId(exam1.getId());
        exam2.setStem(exam1.getStem());
        exam2.setTotalScore(exam1.getTotalScore());
        exam2.setCheckStatusId(exam1.getCheckStatusId());
        exam2.setCategoryId(exam1.getCategoryId());
        exam2.setOutlineCode(exam1.getOutlineCode());
        exam2.setKnowledgePoint(exam1.getKnowledgePoint());
        exam2.setCognitiveLevel(exam1.getCognitiveLevel());
        exam2.setUserIdImport(exam1.getUserIdImport());
        exam2.setLocation1(exam1.getLocation1());
        exam2.setLocation2(exam1.getLocation2());
        exam2.setLocation3(exam1.getLocation3());
        exam2.setLocation1Score(exam1.getLocation1Score());
        exam2.setLocation2Score(exam1.getLocation2Score());
        exam2.setLocation3Score(exam1.getLocation3Score());
        exam2.setMeridian(exam1.getMeridian());
        exam2.setMeridianScore(exam1.getMeridianScore());
        exam2.setMayorCure1(exam1.getMayorCure1());
        exam2.setMayorCure2(exam1.getMayorCure2());
        exam2.setMayorCure3(exam1.getMayorCure3());
        exam2.setMayorCure4(exam1.getMayorCure4());
        exam2.setMayorCure5(exam1.getMayorCure5());
        exam2.setMayorCure6(exam1.getMayorCure6());
        exam2.setMayorCure7(exam1.getMayorCure7());
        exam2.setMayorCure8(exam1.getMayorCure8());
        exam2.setMayorCure1Score(exam1.getMayorCure1Score());
        exam2.setMayorCure2Score(exam1.getMayorCure2Score());
        exam2.setMayorCure3Score(exam1.getMayorCure3Score());
        exam2.setMayorCure4Score(exam1.getMayorCure4Score());
        exam2.setMayorCure5Score(exam1.getMayorCure5Score());
        exam2.setMayorCure6Score(exam1.getMayorCure6Score());
        exam2.setMayorCure7Score(exam1.getMayorCure7Score());
        exam2.setMayorCure8Score(exam1.getMayorCure8Score());
        return exam2;
    }
    /**
     * 十条答案
     * 将终极表试题的值给予一审表试题
     * @param exam1 终极表试题
     * @param exam2 一审表试题
     * @return
     */
    public static ExamAnswer10Original setValueIntoExamOriginal(ExamAnswer10 exam1, ExamAnswer10Original exam2) {
        exam2.setId(exam1.getId());
        exam2.setStem(exam1.getStem());
        exam2.setTotalScore(exam1.getTotalScore());
        exam2.setCheckStatusId(exam1.getCheckStatusId());
        exam2.setCategoryId(exam1.getCategoryId());
        exam2.setOutlineCode(exam1.getOutlineCode());
        exam2.setKnowledgePoint(exam1.getKnowledgePoint());
        exam2.setCognitiveLevel(exam1.getCognitiveLevel());
        exam2.setUserIdImport(exam1.getUserIdImport());
        exam2.setAnswer1(exam1.getAnswer1());
        exam2.setAnswer2(exam1.getAnswer2());
        exam2.setAnswer3(exam1.getAnswer3());
        exam2.setAnswer4(exam1.getAnswer4());
        exam2.setAnswer5(exam1.getAnswer5());
        exam2.setAnswer6(exam1.getAnswer6());
        exam2.setAnswer7(exam1.getAnswer7());
        exam2.setAnswer8(exam1.getAnswer8());
        exam2.setAnswer9(exam1.getAnswer9());
        exam2.setAnswer10(exam1.getAnswer10());
        exam2.setAnswer1Score(exam1.getAnswer1Score());
        exam2.setAnswer2Score(exam1.getAnswer2Score());
        exam2.setAnswer3Score(exam1.getAnswer3Score());
        exam2.setAnswer4Score(exam1.getAnswer4Score());
        exam2.setAnswer5Score(exam1.getAnswer5Score());
        exam2.setAnswer6Score(exam1.getAnswer6Score());
        exam2.setAnswer7Score(exam1.getAnswer7Score());
        exam2.setAnswer8Score(exam1.getAnswer8Score());
        exam2.setAnswer9Score(exam1.getAnswer9Score());
        exam2.setAnswer10Score(exam1.getAnswer10Score());
        return exam2;
    }
    /**
     * 三十条答案
     * 将终极表试题的值给予一审表试题
     * @param exam1 终极表试题
     * @param exam2 一审表试题
     * @return
     */
    public static ExamAnswer30Original setValueIntoExamOriginal(ExamAnswer30 exam1, ExamAnswer30Original exam2) {
        exam2.setId(exam1.getId());
        exam2.setStem(exam1.getStem());
        exam2.setTotalScore(exam1.getTotalScore());
        exam2.setCheckStatusId(exam1.getCheckStatusId());
        exam2.setCategoryId(exam1.getCategoryId());
        exam2.setOutlineCode(exam1.getOutlineCode());
        exam2.setKnowledgePoint(exam1.getKnowledgePoint());
        exam2.setCognitiveLevel(exam1.getCognitiveLevel());
        exam2.setUserIdImport(exam1.getUserIdImport());
        exam2.setAnswer1(exam1.getAnswer1());
        exam2.setAnswer2(exam1.getAnswer2());
        exam2.setAnswer3(exam1.getAnswer3());
        exam2.setAnswer4(exam1.getAnswer4());
        exam2.setAnswer5(exam1.getAnswer5());
        exam2.setAnswer6(exam1.getAnswer6());
        exam2.setAnswer7(exam1.getAnswer7());
        exam2.setAnswer8(exam1.getAnswer8());
        exam2.setAnswer9(exam1.getAnswer9());
        exam2.setAnswer10(exam1.getAnswer10());
        exam2.setAnswer11(exam1.getAnswer11());
        exam2.setAnswer12(exam1.getAnswer12());
        exam2.setAnswer13(exam1.getAnswer13());
        exam2.setAnswer14(exam1.getAnswer14());
        exam2.setAnswer15(exam1.getAnswer15());
        exam2.setAnswer16(exam1.getAnswer16());
        exam2.setAnswer17(exam1.getAnswer17());
        exam2.setAnswer18(exam1.getAnswer18());
        exam2.setAnswer19(exam1.getAnswer19());
        exam2.setAnswer20(exam1.getAnswer20());
        exam2.setAnswer21(exam1.getAnswer21());
        exam2.setAnswer22(exam1.getAnswer22());
        exam2.setAnswer23(exam1.getAnswer23());
        exam2.setAnswer24(exam1.getAnswer24());
        exam2.setAnswer25(exam1.getAnswer25());
        exam2.setAnswer26(exam1.getAnswer26());
        exam2.setAnswer27(exam1.getAnswer27());
        exam2.setAnswer28(exam1.getAnswer28());
        exam2.setAnswer29(exam1.getAnswer29());
        exam2.setAnswer30(exam1.getAnswer30());
        exam2.setAnswer1Score(exam1.getAnswer1Score());
        exam2.setAnswer2Score(exam1.getAnswer2Score());
        exam2.setAnswer3Score(exam1.getAnswer3Score());
        exam2.setAnswer4Score(exam1.getAnswer4Score());
        exam2.setAnswer5Score(exam1.getAnswer5Score());
        exam2.setAnswer6Score(exam1.getAnswer6Score());
        exam2.setAnswer7Score(exam1.getAnswer7Score());
        exam2.setAnswer8Score(exam1.getAnswer8Score());
        exam2.setAnswer9Score(exam1.getAnswer9Score());
        exam2.setAnswer10Score(exam1.getAnswer10Score());
        exam2.setAnswer11Score(exam1.getAnswer11Score());
        exam2.setAnswer12Score(exam1.getAnswer12Score());
        exam2.setAnswer13Score(exam1.getAnswer13Score());
        exam2.setAnswer14Score(exam1.getAnswer14Score());
        exam2.setAnswer15Score(exam1.getAnswer15Score());
        exam2.setAnswer16Score(exam1.getAnswer16Score());
        exam2.setAnswer17Score(exam1.getAnswer17Score());
        exam2.setAnswer18Score(exam1.getAnswer18Score());
        exam2.setAnswer19Score(exam1.getAnswer19Score());
        exam2.setAnswer20Score(exam1.getAnswer20Score());
        exam2.setAnswer21Score(exam1.getAnswer21Score());
        exam2.setAnswer22Score(exam1.getAnswer22Score());
        exam2.setAnswer23Score(exam1.getAnswer23Score());
        exam2.setAnswer24Score(exam1.getAnswer24Score());
        exam2.setAnswer25Score(exam1.getAnswer25Score());
        exam2.setAnswer26Score(exam1.getAnswer26Score());
        exam2.setAnswer27Score(exam1.getAnswer27Score());
        exam2.setAnswer28Score(exam1.getAnswer28Score());
        exam2.setAnswer29Score(exam1.getAnswer29Score());
        exam2.setAnswer30Score(exam1.getAnswer30Score());
        return exam2;
    }
}
